#pragma GCC optimize(2)
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <vector>

using namespace std;
using LL = long long;
const int N = 20;

int n;
LL maxv = 0;
int dis[8][2] = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}};
int g[N][N];
bool st[N][N];

void dfs(int x, int y, int direction, LL num){
    if(st[x][y]){
        maxv = max(maxv, num);
        return;
    }else{
        LL temp = num * 10 + g[x][y];
        st[x][y] = true;
        int nx = (x + dis[direction][0] + n) % n;
        int ny = (y + dis[direction][1] + n) % n;
        dfs(nx, ny, direction, temp);
    }
}

int main(){
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> n;
    string s;
    for(int i = 0; i < n; i ++){
        cin >> s;
        for(int j = 0; j < n; j ++){
            g[i][j] = s[j] - '0';
        }
    }

    for(int i = 0; i < n; i ++){
        for(int j = 0; j < n; j ++){
            for(int k = 0; k < 8; k ++){
                memset(st, 0, sizeof st);
                dfs(i, j, k, 0);
            }
        }
    }

    cout << maxv;


    return 0;
}